package com.example.administrator.x3picturetransliteration.Home.Activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.example.administrator.x3picturetransliteration.Base.BaseActivity;
import com.example.administrator.x3picturetransliteration.Base.BasePresenter;
import com.example.administrator.x3picturetransliteration.Home.Dialog.PreservationDialog;
import com.example.administrator.x3picturetransliteration.Home.Dialog.SaveDraftDialog;
import com.example.administrator.x3picturetransliteration.Home.Dialog.ShareDialog;
import com.example.administrator.x3picturetransliteration.Home.Presenter.Activity.TranslatePresenter;
import com.example.administrator.x3picturetransliteration.R;
import com.example.administrator.x3picturetransliteration.RxBus;
import com.example.administrator.x3picturetransliteration.Util.TimeStampUtil;
import com.example.administrator.x3picturetransliteration.Util.ToastUtil;
import com.example.administrator.x3picturetransliteration.Util.UmengShareUtil;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 翻译
 * Created by Administrator on 2019/8/21.
 */

public class TranslateActivity extends BaseActivity{

    /**
     * 显示分享按钮且是无保存文档消息的时候
     * @param imgs 翻译图片 保存文档时候用的
     * @param result 需要翻译的内容
     * */
    public static void startTranslateActivity(Context context,String imgs,String result){

        Intent intent1 = new Intent(context, TranslateActivity.class);
        intent1.putExtra("type","3");
        intent1.putExtra("share","Translate");
        intent1.putExtra("imgs",imgs);
        intent1.putExtra("result",result);
        context.startActivity(intent1);
    }

    /**
     * 修改翻译文档的时候用的
     * @param fromLang 被翻译语种类型名称和代码｛"name":"name","code":"code"｝
     * @param toLang 文档内容
     * @param translate 翻译内容
     * @param name 文档内容
     * @param wordid 文档id
     * @param draftid 草稿id
     * @param imgs 翻译图片 保存文档时候用的
     * @param result 需要翻译的内容
     * */
    public static void startTranslateActivity(Context context,
                                              String fromLang,String toLang,
                                              String translate,String name,
                                              String wordid,String draftid,
                                              String imgs,String result){

        Intent intent1 = new Intent(context, TranslateActivity.class);
        intent1.putExtra("type","3");
        intent1.putExtra("fromLang",fromLang);
        intent1.putExtra("toLang",toLang);
        intent1.putExtra("translate",translate);
        intent1.putExtra("name",name);
        intent1.putExtra("wordid",wordid);
        intent1.putExtra("draftid",draftid);
        intent1.putExtra("imgs",imgs);
        intent1.putExtra("result",result);
        context.startActivity(intent1);
    }


    @BindView(R.id.TitleText)TextView TitleText;
    @BindView(R.id.LButton)ImageView LButton;
    @BindView(R.id.text)TextView text;

    @BindView(R.id.LanguageAText)TextView LanguageAText;
    @BindView(R.id.LanguageBText)TextView LanguageBText;

    @BindView(R.id.TranslateAText)TextView TranslateAText;
    @BindView(R.id.TranslateBText)TextView TranslateBText;

    @BindView(R.id.fxView)View fxView;

    private String from = "cn"; //原语言
    private String to = "en"; //翻译后语言

    private String wordid = null; //文档id用来判断

    private String draftid = null; //草稿id

    private TranslatePresenter mTranslatePresenter;

    @Override
    public int intiLayout() {
        return R.layout.home_activity_translate;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TitleText.setText("翻译");
        LButton.setImageResource(R.mipmap.home_backw);
        text.setText("保存");

        mTranslatePresenter = new TranslatePresenter(this,mCompositeSubscriptions);

        if ("Translate".equals(getIntent().getStringExtra("share"))){

            fxView.setVisibility(View.VISIBLE);
            TitleText.setText("翻译结果页");
        }

        if (getIntent().getStringExtra("wordid")!=null)wordid = getIntent().getStringExtra("wordid");
        if (getIntent().getStringExtra("draftid")!=null)draftid = getIntent().getStringExtra("draftid");

        TranslateAText.setText(getIntent().getStringExtra("result")!=null?getIntent().getStringExtra("result") + "" :"");

    }

    @Override
    protected void onResume() {
        super.onResume();

        if (getIntent().getStringExtra("translate")!=null && getIntent().getStringExtra("fromLang")!=null && getIntent().getStringExtra("toLang")!=null){

            TranslateBText.setText(getIntent().getStringExtra("translate") + "");

            Map<String,String> fromLang = (Map<String,String>)JSON.parse(getIntent().getStringExtra("fromLang") + "");
            Map<String,String> toLang = (Map<String,String>)JSON.parse(getIntent().getStringExtra("toLang") + "");

            LanguageAText.setText(fromLang.get("name"));
            LanguageBText.setText(toLang.get("name"));

            from = fromLang.get("code");
            to = toLang.get("code");

        }else {

            ots();
        }
    }

    /** 保存 */
    @OnClick(R.id.text)
    public void textClick(){

        final Map<String,String> map = new HashMap<>();

        if (wordid!=null)map.put("id",wordid);
        map.put("type",getIntent().getStringExtra("type"));
        map.put("imgs",getIntent().getStringExtra("imgs"));
        map.put("result",getIntent().getStringExtra("result"));
        map.put("translate",TranslateBText.getText() + "");

        Map<String,String> fromLang = new HashMap<>();
        fromLang.put("name",LanguageAText.getText() + "");
        fromLang.put("code",from);

        Map<String,String> toLang = new HashMap<>();
        toLang.put("name",LanguageBText.getText() + "");
        toLang.put("code",to);

        map.put("fromLang",JSON.toJSONString(fromLang));
        map.put("toLang",JSON.toJSONString(toLang));

        new PreservationDialog(this, new PreservationDialog.hd() {
            @Override
            public void hdff(String content) {

                map.put("name",content);

                mTranslatePresenter.addwork(map, new BasePresenter.Callback() {
                    @Override
                    public void getData(Object data) {

                        wordid = (String)data;
                    }
                });
            }

            @Override
            public String nameText() {
                return getIntent().getStringExtra("name")!=null?getIntent().getStringExtra("name"):"OCR " + new TimeStampUtil().stampToDate(new Date().getTime());
            }
        }).show();
    }

    /** 原文选择 */
    @OnClick(R.id.LanguageAView)
    public void LanguageAViewClick(){

        getLanguage(LanguageAText,true);
    }

    /** 翻译选择 */
    @OnClick(R.id.LanguageBView)
    public void LanguageBViewClick(){

        getLanguage(LanguageBText,false);
    }

    /** 返回 */
    @OnClick(R.id.LButton)
    public void LButtonClick(){

        if (wordid!=null)finish();

        new SaveDraftDialog(this, new SaveDraftDialog.hd() {
            @Override
            public void ok() {

                bccgx();
                finish();
            }

            @Override
            public void no() {

                finish();
            }
        }).show();
    }

    @Override
    public void onBackPressed() {
        //super.onBackPressed();

        if (wordid!=null)finish();

        new SaveDraftDialog(this, new SaveDraftDialog.hd() {
            @Override
            public void ok() {

                bccgx();
                finish();
            }

            @Override
            public void no() {

                finish();
            }
        }).show();
    }

    /** 分享 */
    @OnClick(R.id.fxView)
    public void fxViewClick(){

        if (wordid==null){

            new ToastUtil(this).getToast(false,"还没有保存").show();
            return;
        }

        new ShareDialog(this, true, new ShareDialog.hd() {
            @Override
            public void hdff(final SHARE_MEDIA type, String word) {

                Map<String,String> map = new HashMap<>();
                map.put("id",wordid);
                map.put("exportType",word);

                mTranslatePresenter.export(map, new BasePresenter.Callback() {
                    @Override
                    public void getData(Object data) {


                        Share(type,data + "");
                    }
                });

                Share(type,word);
            }
        }).show();
    }

    /** 语言选择 */
    public void getLanguage(final TextView text, final boolean judge){


        final OptionsPickerView pvOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3 ,View v) {

                text.setText(getName().get(options1));

                if (judge){

                    from = getDm().get(options1);

                }else {

                    to = getDm().get(options1);
                    ots();
                }
            }
        })
        .setCancelColor(Color.parseColor("#61000000"))
        .build();
        pvOptions.setNPicker(getName(),null,null);
        pvOptions.show();

    }

    private List<String> getName(){

        String data = "中文(简体),希伯来语,帕皮阿门托语,中文(繁体),印地语,波兰语,英语,克罗地亚语,普什图语,日语,海地克里奥尔语,罗马尼亚语,韩语,匈牙利语,信德语,俄语,亚美尼亚语,僧伽罗语,法语,印尼语,斯洛伐克语,西班牙语,伊博语,斯洛文尼亚语,阿拉伯语,冰岛语,萨摩亚语,葡萄牙语,意大利语,修纳语,南非荷兰语,印尼爪哇语,索马里语,阿姆哈拉语,格鲁吉亚语,阿尔巴尼亚语,阿塞拜疆语,哈萨克语,塞尔维亚语,巴什基尔语,高棉语,塞索托语,白俄罗斯语,卡纳达语,印尼巽他语,保加利亚语,库尔德语,瑞典语,孟加拉语,吉尔吉斯语,斯瓦希里语,波斯尼亚语,拉丁语,泰米尔语,加泰罗尼亚语,卢森堡语,泰卢固语,宿务语,老挝语,塔吉克语,科西嘉语,立陶宛语,泰语,捷克语,拉脱维亚语,藏语,威尔士语,马尔加什语,汤加语,丹麦语,马里语,土耳其语,德语,毛利语,鞑靼语,希腊语,马其顿语,塔希提语,世界语,马拉雅拉姆语,乌德穆尔特语,爱沙尼亚语,蒙古语(西里尔),乌克兰语,巴斯克语,马拉地语,乌尔都语,波斯语,山地马里语,乌兹别克语,芬兰语,马来语,越南语,菲律宾语,马耳他语,南非科萨语,斐济语,白苗文,意第绪语,弗里西语,缅甸语,约鲁巴语,爱尔兰语,尼泊尔语,尤卡坦玛雅语,苏格兰盖尔语,荷兰语,广东话,加利西亚,挪威语,南非祖鲁语,古吉拉特语,齐切瓦语,豪萨语,克雷塔罗奥托米语,夏威夷语,旁遮普语";

        return Arrays.asList(data.split(","));
    }

    private List<String> getDm(){

        String data = "cn,he,pap,cht,hi,pl,en,hr,ps,ja,ht,ro,ko,hu,sd,ru,hy,si,fr,id,sk,es,ig,sl,ar,is,sm,pt,it,sn,af,jv,so,am,jy,sq,az,ka,sr,ba,km,st,be,kn,su,bg,ku,sv,bn,ky,sw,bs,la,ta,ca,lb,te,ceb,lo,tg,co,lt,th,cs,lv,ti,cy,mg,to,da,mhr,tr,de,mi,tt,el,mk,ty,eo,ml,udm,et,mn,uk,eu,mr,ur,fa,mrj,uz,fi,ms,vi,fil,mt,xh,fj,mww,yi,fy,my,yo,ga,ne,yua,gd,nl,yue,gl,no,zu,gu,ny,ha,otq,haw,pa";

        return Arrays.asList(data.split(","));
    }

    private void ots(){

        Map<String,String> map = new HashMap<>();

        map.put("text",TranslateAText.getText() + "");
        map.put("from",from);
        map.put("to",to);

        mTranslatePresenter.ots(map, new BasePresenter.Callback() {
            @Override
            public void getData(Object data) {

                TranslateBText.setText(data + "");
            }
        });

    }

    @Override
    protected void onDestroy() {

        super.onDestroy();
    }

    private void bccgx(){

        Map<String,String> map = new HashMap<>();

        if (draftid!=null)map.put("id",draftid);
        map.put("type",getIntent().getStringExtra("type"));
        map.put("name","CG " + new TimeStampUtil().stampToDate(new Date().getTime()));
        map.put("imgs",getIntent().getStringExtra("imgs"));
        map.put("result",getIntent().getStringExtra("result"));
        map.put("translate",TranslateBText.getText() + "");

        Map<String,String> fromLang = new HashMap<>();
        fromLang.put("name",LanguageAText.getText() + "");
        fromLang.put("code",from);

        Map<String,String> toLang = new HashMap<>();
        toLang.put("name",LanguageBText.getText() + "");
        toLang.put("code",to);

        map.put("fromLang",JSON.toJSONString(fromLang));
        map.put("toLang",JSON.toJSONString(toLang));

        if (wordid==null) {

            RxBus.getInstance().post(AppActivity.class, JSON.toJSONString(map));
        }

    }

    private void Share(SHARE_MEDIA mSHARE_MEDIA,String url){

        UmengShareUtil mUmengShareUtil = new UmengShareUtil(this);
        mUmengShareUtil.setUrl(url);
        mUmengShareUtil.setTitle("识别结果");
        mUmengShareUtil.setThumb(R.mipmap.home_logo);
        mUmengShareUtil.setDescription("表格识别识别结果");
        mUmengShareUtil.Share(mSHARE_MEDIA);
    }

}
